<?php

namespace app\models;

use app\utils\BaseUtils;
use yii;
use app\components\BaseActiveRecord;
use yii\helpers\Html;

/**
 * This is the model class for table "{{%activity}}".
 *
 * @property integer $id
 * @property integer $uid
 * @property integer $cid
 * @property integer $type
 * @property string $title
 * @property integer $start_time
 * @property integer $end_time
 * @property string $company
 * @property integer $province_id
 * @property integer $city_id
 * @property string $province
 * @property string $city
 * @property string $address
 * @property string $thumbs
 * @property string $content
 * @property string $tag
 * @property string $remark
 * @property integer $number
 * @property integer $apply_number
 * @property string $salary
 * @property integer $price
 * @property integer $likes
 * @property integer $favorites
 * @property integer $views
 * @property integer $comments
 * @property integer $status
 * @property integer $is_new
 * @property integer $is_top
 * @property integer $is_hot
 * @property integer $utime
 * @property integer $ctime
 *
 * @property User $user
 */
class Activity extends BaseActiveRecord {

    const STATUS_VERIFY = 0;
    const STATUS_VERIFY_DENY = 5;
    const STATUS_PENDING = 1;
    const STATUS_RUNNING = 2;
    const STATUS_END = 3;
    const STATUS_DELETE = 4;

    const CID_SHA_LONG = 1;
    const CID_SAI_SHI = 2;
    const CID_ZHI_YUAN_ZHE = 3;
    const CID_JOB = 4;
    const CID_PRACTICE = 5;

    const TYPE_ACTIVITY = 1;
    const TYPE_JOB = 2;

    public static $statusLabels = [
        self::STATUS_VERIFY => '待审核',
        self::STATUS_VERIFY_DENY => '审核未通过',
        self::STATUS_PENDING => '报名中',
        self::STATUS_RUNNING => '进行中',
        self::STATUS_END => '已结束',
        self::STATUS_DELETE => '已删除',
    ];

    public static $cidLabels = [
        self::CID_SHA_LONG => '讲座沙龙',
        self::CID_SAI_SHI => '大学生赛事',
        self::CID_ZHI_YUAN_ZHE => '志愿者义工',
        self::CID_JOB => '实习生',
        self::CID_PRACTICE => '兼职',
    ];

    public static $typeLabels = [
        self::TYPE_ACTIVITY => '活动',
        self::TYPE_JOB => '实习生兼职',
    ];

    public static $statusColors = [
        self::STATUS_VERIFY => 'warning',
        self::STATUS_DELETE => 'danger',
        self::STATUS_VERIFY_DENY => 'danger',
        self::STATUS_PENDING => 'success',
        self::STATUS_RUNNING => 'info',
        self::STATUS_END => 'warning',
    ];

    public static $cidTag = [
        self::CID_SHA_LONG => '',
        self::CID_SAI_SHI => 'card-tag-blue',
        self::CID_ZHI_YUAN_ZHE => 'card-tag-green',
        self::CID_JOB => '',
        self::CID_PRACTICE => 'card-tag-green',
    ];

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%activity}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['content', 'title', 'thumbs', 'cid', 'start_time', 'end_time', 'type', 'province', 'city'], 'required'],

            ['type', 'in', 'range' => [static::TYPE_JOB, static::TYPE_ACTIVITY]],
            ['cid', 'in', 'range' => array_keys(static::$cidLabels)],

            [['uid', 'cid', 'type', 'province_id', 'city_id', 'number', 'apply_number', 'price', 'likes', 'favorites', 'views', 'comments', 'status', 'is_new', 'is_top', 'is_hot', 'utime', 'ctime'], 'integer'],
            [['content'], 'string'],
            [['title', 'company', 'tag', 'remark'], 'string', 'max' => 160],
            [['address'], 'string', 'max' => 220],
            [['thumbs'], 'string', 'max' => 500],
            [['salary', 'province', 'city'], 'string', 'max' => 60]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'uid' => '发布者',
            'cid' => 'Cid',
            'type' => '类型',
            'title' => $this->getIsActivity() ? '主题' : '职位名称',
            'start_time' => '开始时间',
            'end_time' => '结束时间',
            'company' => $this->getIsActivity() ? '主办方' : '公司名称',
            'province' => '省份',
            'city' => '城市',
            'address' => '详细地址',
            'thumbs' => '海报图',
            'content' => $this->getIsActivity() ? '活动说明' : '职位简介',
            'tag' => '学历',
            'number' => '活动人数',
            'apply_number' => '报名人数',
            'salary' => '薪资',
            'price' => '价格',
            'likes' => '点赞数',
            'favorites' => '收藏数',
            'views' => '浏览数',
            'comments' => '评论数',
            'status' => '状态',
            'is_new' => '最新',
            'is_hot' => '最热',
            'is_top' => '置顶',
            'utime' => 'Utime',
            'ctime' => '发布时间',
        ];
    }

    public function getUser() {
        return $this->hasOne(User::className(), ['id' => 'uid']);
    }

    public function beforeValidate() {
        if ($this->isNewRecord) {
            if ($this->cid == static::CID_JOB) {
                $this->start_time = $this->end_time = 0;
            }
        }

        if ($this->start_time) {
            $this->start_time = strtotime($this->start_time);
            $this->end_time = strtotime($this->end_time);
            if ($this->start_time > $this->end_time) {
                $this->addError('start_time', '开始时间不能大于结束时间');
                return FALSE;
            }
        } elseif ($this->cid != self::CID_JOB) {
            $this->addError('start_time', '请选择时间');
        }

        if (is_array($this->thumbs) && !empty($this->thumbs)) {
            $this->thumbs = yii\helpers\Json::encode($this->thumbs);
        }
        return parent::beforeValidate();
    }

    public function beforeSave($insert) {
        if ($insert) {
            $this->status = static::STATUS_VERIFY;
        }
        if (empty($this->number)) {
            $this->number = 0;
        }
        if (empty($this->price)) {
            $this->price = 0;
        }
        return parent::beforeSave($insert);
    }

    public function getThumbs() {
        return yii\helpers\Json::decode($this->thumbs);
    }

    public function getThumb($default = '') {
        $thumbs = yii\helpers\Json::decode($this->thumbs);
        $thumb = empty($thumbs) ? $default : current($thumbs);
        return BaseUtils::formatImage($thumb);
    }

    public function getContentImage() {
        $thumbs = yii\helpers\Json::decode($this->thumbs);
        if (empty($thumbs) || count($thumbs) < 2) {
            return [];
        }
        array_shift($thumbs);
        return $thumbs;
    }

    public function getIsApply($user, $isGuest = TRUE) {
        if (!$user) {
            return $isGuest;
        }

        /* @var $user User */
        if (!$user->getIsStudent()) {
            return FALSE;
        }

        if ($this->number > 0 && $this->apply_number >= $this->number) {
            return FALSE;
        }

        if ($this->start_time > 0 && $this->start_time < time()) {
            return FALSE;
        }

        if (ActivityApply::getIsApply($this->id, $user->id)) {
            return FALSE;
        }
        return TRUE;
    }

    public function getIsVerify() {
        return intval($this->status) === intval(self::STATUS_VERIFY);
    }

    public function getIsVerifyDeny() {
        return intval($this->status) === intval(self::STATUS_VERIFY_DENY);
    }

    public function getIsPending() {
        return intval($this->status) === intval(self::STATUS_PENDING);
    }

    public function getIsEnd() {
        return intval($this->status) === intval(self::STATUS_END);
    }

    public function getIsDelete() {
        return intval($this->status) === intval(self::STATUS_DELETE);
    }

    public function getIsActivity() {
        return intval($this->type) === intval(self::TYPE_ACTIVITY);
    }

    public function getIsNew() {
        return intval($this->is_new) === intval(self::STATUS_YES);
    }

    public function getIsHot() {
        return intval($this->is_hot) === intval(self::STATUS_YES);
    }

    public function getIsTop() {
        return intval($this->is_top) === intval(self::STATUS_YES);
    }

    public function getIsEdit(User $user) {
        if ($user->id !== $this->uid) {
            return FALSE;
        }
        if ($this->end_time <= 0) {
            return TRUE;
        }
        return $this->end_time > time();
    }

    public function getIsNewLabel($url) {
        $class = $this->getIsNew() ? 'fa-check text-green' : 'fa-close';
        $title = $this->getIsNew() ? '取消最新' : '设置最新';
        return Html::tag('i', '', [
                'class' => "fa get {$class}",
                'data-toggle' => 'tooltip',
                'title' => $title,
                'style' => 'cursor:pointer;',
                'href' => $url]
        );
    }

    public function getIsHotLabel($url) {
        $class = $this->getIsHot() ? 'fa-check text-green' : 'fa-close';
        $title = $this->getIsHot() ? '取消最热' : '设置最热';

        return Html::tag('i', '', [
                'class' => "fa get {$class}",
                'data-toggle' => 'tooltip',
                'title' => $title,
                'style' => 'cursor:pointer;',
                'href' => $url
            ]
        );
    }

    public function getIsTopLabel($url) {
        $class = $this->getIsTop() ? 'fa-check text-green' : 'fa-close';
        $title = $this->getIsTop() ? '取消置顶' : '置顶';

        return Html::tag('i', '', [
                'class' => "fa get {$class}",
                'data-toggle' => 'tooltip',
                'title' => $title,
                'style' => 'cursor:pointer;',
                'href' => $url
            ]
        );
    }

    public function applySuccess() {
        $this->apply_number = $this->apply_number + 1;
        return $this->save();
    }

    public function formatNumber() {
        if ($this->number > 0) {
            return "限{$this->number}人";
        }
        return '不限';
    }

    public function formatPrice() {
        if ($this->price > 0) {
            return "{$this->price}元";
        }
        return '免费';
    }

    public function formatTime() {
        if (date('m-d', $this->end_time) === date('m-d', $this->start_time)) {
            return date('m/d', $this->start_time);
        }
        return date('m/d', $this->start_time) . '~' . date('m/d', $this->end_time);
    }

    public function getFullAddress() {
        return "{$this->province}{$this->city}{$this->address}";
    }

    public function getProvinceAndCity() {
        return "{$this->province}{$this->city}";
    }

    public function getApplyLabel($status) {
        $statuses = [
            ActivityApply::STATUS_PENDING => '待审核',
            ActivityApply::STATUS_ALLOW => '通过',
            ActivityApply::STATUS_DENY => '不合适',
        ];

        $statusColors = [
            ActivityApply::STATUS_PENDING => '13D781',
            ActivityApply::STATUS_ALLOW => '13D781',
            ActivityApply::STATUS_DENY => 'D7132E',
        ];
        if(!isset($statuses[$status])){
            return '';
        }
        return Html::tag('font', $statuses[$status], ['color' => "#{$statusColors[$status]}"]);
    }
}
